package com.dsg.controller.apply;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dsg.common.constants.MessageConstants;
import com.dsg.common.dto.AjaxResult;
import com.dsg.common.util.UUIDGenerator;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.controller.base.BaseController;
import com.dsg.entity.apply.DsgApplyDdaSub;
import com.dsg.service.apply.IDsgApplyDdaSubService;
import com.dsg.util.ExcelIteratorReader;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;

/**
 * <p>
 * DDA設立記錄子表 前端控制器
 * </p>
 *
 * @author system
 * @since 2020-10-06
 */
@RestController
@RequestMapping("/apply/dsgApplyDdaSub")
public class DsgApplyDdaSubController extends BaseController {

    @Autowired
    private IDsgApplyDdaSubService dsgApplyDdaSubService;


    /**
     * 查询列表
     *
     * @param id
     * @return
     */
    @GetMapping(value = "{id}/list")
    @ApiOperation("查询列表")
    public AjaxResult list(@PathVariable String id) {
        QueryWrapper<DsgApplyDdaSub> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PID", id);
        queryWrapper.orderByDesc("UPLOAD_TIME");
        List<DsgApplyDdaSub> dsgApplyDdaSubList = dsgApplyDdaSubService.list(queryWrapper);
        return json(MessageConstants.SSO_STATUS_SUCCESS, dsgApplyDdaSubList);
    }


    /**
     * 更新狀態
     *
     * @param dsgApplyDdaSub
     * @return
     */
    @PutMapping(value = "/updateDdaStatus")
    @ApiOperation("更新狀態")
    public AjaxResult updateDdaStatus(@RequestBody DsgApplyDdaSub dsgApplyDdaSub) {
        dsgApplyDdaSubService.updateDdaStatus(dsgApplyDdaSub);
        return result(MessageConstants.SSO_STATUS_SUCCESS);
    }

    /**
     * 新增
     *
     * @param dsgApplyDdaSub
     * @return
     */
    @PostMapping(value = "/add")
    @ApiOperation("新增")
    public AjaxResult add(@RequestBody DsgApplyDdaSub dsgApplyDdaSub) {
        dsgApplyDdaSub.setOid(UUIDGenerator.getUUID());
        boolean flag = dsgApplyDdaSubService.save(dsgApplyDdaSub);
        if (flag) {
            return json(MessageConstants.SSO_STATUS_SUCCESS, dsgApplyDdaSub);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }

    /**
     * 落銀行
     *
     * @return
     */
    @GetMapping(value = "/downloadFile")
    @ApiOperation("落銀行")
    @Transactional(rollbackFor = Exception.class)
    public void downloadFile(@ApiParam(value = "id", name = "id", required = true) @RequestParam String id,
                             HttpServletResponse response) throws Exception {
        dsgApplyDdaSubService.downloadFile(response, id);
    }

    /**
     * 上載
     *
     * @return
     */
    @PostMapping(value = "/importFile")
    @ApiOperation("上載")
    public AjaxResult importFile(DsgApplyDdaSub dsgApplyDdaSub,
                                 @ApiParam(value = "file", name = "file", required = true) @RequestParam("file") MultipartFile file) throws Exception {
        if (StringUtil.isEmpty(dsgApplyDdaSub.getOid())) {
            return setErrorJson("主键不能为空!");
        }
        File ddaFile = ExcelIteratorReader.multipartToFile(file);
        dsgApplyDdaSubService.importFile(ddaFile, dsgApplyDdaSub);
        return result(MessageConstants.SSO_STATUS_SUCCESS);
    }
}
